﻿package com.taihua_tech.basic.service;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.taihua_tech.basic.dao.BookDao;
import com.taihua_tech.basic.entity.Book;
import com.taihua_tech.basic.util.NoteResult;
import com.taihua_tech.basic.util.NoteUtil;
 
//注入bookDao
//调用bookDao.findByUserId,将结果封装成NoteResult

@Service("bookService")
@Transactional
public class BookServiceImpl implements BookService{
	@Resource
	private BookDao bookDao;
	
	@Transactional
	public NoteResult loadUserBooks(String userId) {
		List<Book> list = 
			bookDao.findByUserId(userId);
		NoteResult result = new NoteResult();
		result.setStatus(0);
		result.setMsg("查询笔记本列表成功");
		result.setData(list);
		return result;
	}

	public NoteResult addBook(
		String userId, String bookName) {
		Book book = new Book();
		book.setCn_notebook_name(bookName);//设置笔记本名
		book.setCn_user_id(userId);//设置用户ID
		book.setCn_notebook_type_id("5");//normal
		book.setCn_notebook_desc("");
		String bookId = NoteUtil.createId();
		book.setCn_notebook_id(bookId);//设置笔记本ID
		Timestamp time = new Timestamp(
			System.currentTimeMillis());
		book.setCn_notebook_createtime(time);//设置创建时间
		bookDao.save(book);//插入cn_notebook
		NoteResult result = new NoteResult();
		result.setStatus(0);
		result.setMsg("创建笔记本成功");
		result.setData(bookId);//返回笔记本ID
		return result;
	}

	public NoteResult loadAllBooks() {
		List list = bookDao.findAllBooks();
		NoteResult result = new NoteResult();
		result.setStatus(0);
		result.setMsg("查询成功");
		result.setData(list);
		return result;
	}

}
